package com.ruoyi.system.dubbo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.constant.CacheNames;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.RemoteSysConfigService;
import com.ruoyi.system.api.model.SysConfigVo;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author Zzneko
 * @Date 2025/7/16 09:30
 * @Descprition
 **/
@DubboService
@RequiredArgsConstructor
public class RemoteSysConfigServiceImpl implements RemoteSysConfigService {

    private final SysConfigMapper baseMapper;
    @Override
    public SysConfigVo selectConfigById(Long configId) {
        SysConfig sysConfig = baseMapper.selectById(configId);
        SysConfigVo bean = BeanUtil.toBean(sysConfig, SysConfigVo.class);
        return bean;
    }

    @Cacheable(cacheNames = CacheNames.SYS_CONFIG, key = "#configKey")
    @Override
    public String selectConfigByKey(String configKey) {
        SysConfig retConfig = baseMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
            .eq(SysConfig::getConfigKey, configKey));
        if (ObjectUtil.isNotNull(retConfig)) {
            return retConfig.getConfigValue();
        }
        return StringUtils.EMPTY;
    }

    @Override
    public List<SysConfigVo> selectConfigList(SysConfigVo config) {
        Map<String, Object> params = config.getParams();
        LambdaQueryWrapper<SysConfig> lqw = new LambdaQueryWrapper<SysConfig>()
            .like(StringUtils.isNotBlank(config.getConfigName()), SysConfig::getConfigName, config.getConfigName())
            .eq(StringUtils.isNotBlank(config.getConfigType()), SysConfig::getConfigType, config.getConfigType())
            .like(StringUtils.isNotBlank(config.getConfigKey()), SysConfig::getConfigKey, config.getConfigKey())
            .between(params.get("beginTime") != null && params.get("endTime") != null,
                SysConfig::getCreateTime, params.get("beginTime"), params.get("endTime"));
        return baseMapper.selectList(lqw).stream().map(x->{
            SysConfigVo bean = BeanUtil.toBean(x, SysConfigVo.class);
            return bean;
        }).collect(Collectors.toList());
    }

    @Override
    public String insertConfig(SysConfigVo config) {
        return null;
    }

    @Override
    public String updateConfig(SysConfigVo config) {
        return null;
    }

    @Override
    public void deleteConfigByIds(Long[] configIds) {

    }

    @Override
    public void loadingConfigCache() {

    }

    @Override
    public void clearConfigCache() {

    }

    @Override
    public void resetConfigCache() {

    }

    @Override
    public boolean checkConfigKeyUnique(SysConfigVo config) {
        return false;
    }
}
